package com.feifan.monthhelper.calculate;

import com.blankj.utilcode.util.LogUtils;
import com.feifan.monthhelper.bean.db.DayRecordBean;
import com.feifan.monthhelper.constant.Constant;

import java.util.Date;

/**
 * 排卵期算法
 */
public class OvulatoryDayCalculate extends AbstractMenstrualCalculate<DayRecordBean> {

    @Override
    boolean canCalculate(DayRecordBean request) {
        long requestDay = request.createDate.getTime()/(1000 * 60 * 60 * 24);

        long menstrualDay = (getDate().getTime()/(1000 * 60 * 60 * 24));
        if(requestDay >= menstrualDay) {
            return false;
        }
        // 经期开始时间 - 20 < 排卵期 < 经menstrualDay - 期开始时间 - 9
        return 9 < menstrualDay - requestDay && menstrualDay - requestDay < 20;
//        while (requestDay >= menstrualDay) {
//            //如果计算日期在经期开始日期之前，则将经期时间切换到下一个预估周期开始日期
//            menstrualDay += getSpan_period();
//        }
//        //排卵期--月经来潮前的第14天，为了保险起见把这一天的前5天和后4天连同排卵期在内的共十天称为排卵期
//        // 经期开始时间 - 20 < 排卵期 < 经期开始时间 - 9
////        LogUtils.d("OvulatoryDayCalculate = " + getOffset(requestDay, menstrualDay));
//        return 9 < getOffset(requestDay, menstrualDay) && getOffset(requestDay, menstrualDay) < 20;
    }

    //取计算日期和经期开始日期对两个周期间隔取模
    private long getOffset(long requestDay, long menstrualDay) {
        System.out.println("SafeDayCalculate getSpan_period = " + getSpan_period() + "  getMenstrual_period() = " + getMenstrual_period() + "  getOffset = " + Math.abs(requestDay - menstrualDay) % (getSpan_period()));
        return Math.abs(requestDay - menstrualDay) % (getSpan_period());
    }

    @Override
    DayRecordBean calculate(DayRecordBean request) {
        request.state = Constant.MenstrualState.OVULATORY_DAY;
        return request;
    }
}
